package com.blankm.codec_tools;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ResourceUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.blankm.codec_tools.databinding.ActivityDoublePlayerBinding;

import cn.blankm.video.player.VideoPlayer;

import java.util.ArrayList;
import java.util.List;

public class DoublePlayerActivity extends AppCompatActivity implements View.OnClickListener {


    private final String TAG = DoublePlayerActivity.class.getSimpleName();

    ActivityDoublePlayerBinding binding;

    private List<VideoPlayer> mVideoViews = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        VideoPlayerConfig playerConfig = VideoPlayerConfig.newBuilder()
//                .setBuriedPointEvent(new MyBuriedPointEvent()).build();
//        VideoViewManager.setConfig(playerConfig);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_double_player);
        //复制视频
        copyVideo();
        binding.btnPlay.setOnClickListener(this);
        binding.btnStop.setOnClickListener(this);
        VideoLogUtils.setIsLog(true);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_play:
                playerVideo();
                break;
            case R.id.btn_stop:
                binding.videoPlayer.release();
                binding.videoPlayer2.release();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        binding.videoPlayer.release();
        binding.videoPlayer2.release();
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding.videoPlayer.release();
        binding.videoPlayer2.release();
    }

    private void playerVideo() {
        try {
            binding.videoPlayer.setUrl("sdcard/codec_tools/test_3.ts");
            binding.videoPlayer.start();

            binding.videoPlayer2.postDelayed(new Runnable() {
                @Override
                public void run() {
                    binding.videoPlayer2.setUrl("sdcard/codec_tools/test_4.ts");
                    binding.videoPlayer2.start();
                }
            }, 1000);


        } catch (Exception exception) {
            exception.printStackTrace();
        }

    }


    private void copyVideo() {

        ThreadUtils.executeByIo(new ThreadUtils.SimpleTask<Boolean>() {
            @Override
            public Boolean doInBackground() throws Throwable {
                String dir = "sdcard/codec_tools";
                if (FileUtils.isFile(dir + "/test_3.ts")
                        && FileUtils.isFile(dir + "/test_4.ts")) {
                    LogUtils.eTag(TAG, "文件存在");
                    return true;
                }

                if (FileUtils.isDir(dir)) {
                    LogUtils.eTag(TAG, "文件夹存在");
                } else {
                    LogUtils.eTag(TAG, "文件夹不存在");
                    boolean a = FileUtils.createOrExistsDir(dir);
                    LogUtils.eTag(TAG, "文件夹创建" + (a ? "成功" : "失败"));
                }

                return ResourceUtils.copyFileFromAssets("video", dir);
            }

            @Override
            public void onSuccess(Boolean result) {
                LogUtils.eTag(TAG, result ? "文件复制成功" : "文件复制失败");
            }
        });


    }
}